package com.fsts.server;

import java.math.BigInteger;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import com.fsts.entity.PrivateKey;
import com.fsts.entity.PublicKey;
import com.fsts.util.JDBCUtil;

/**
 * User类与数据库的交互层
 * 
 * @method 用于提供，用户的注册，验证等功能
 * @version 1.0
 * @since JDK1.6(建议)
 */
public class UserDao {

	/**
	 * 检查最后一个ID
	 * 
	 * @return
	 */
	public int lastId() {
		int id = 0;
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			// 2.连数据库
			con = JDBCUtil.getConn();
			// 3.获取数据库操作对象(SQL)
			stmt = con.createStatement();
			// 4.操作数据库(查询)
			String sql = "select max(user_id) from user";
			rs = stmt.executeQuery(sql);
			if (rs.next()) {
				id = rs.getInt(1);
			}
		} catch (Exception e) {
			// e.printStackTrace();
			System.out.println("操作失败");
		} finally {
			// 6.关闭JDBC对象
			JDBCUtil.close(rs, stmt, con);
		}
		return id;

	}

	/**
	 * 注册用户
	 * 
	 * @param user
	 * @return
	 */
	public String register(String user) {
		TCDao td = new TCDao();
		String u[] = user.split(",");
		String id = lastId() + 1 + "";
		/** 处理公钥 */
		BigInteger pk = new BigInteger(u[4]); //
		pk = pk.subtract(new BigInteger(id));

		PrivateKey privateKey = td.getPrivateKey();
		BigInteger d = privateKey.getD();

		PublicKey publicKey = td.getPublicKey();
		BigInteger n = publicKey.getN();
		pk = pk.modPow(d, n);

		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			// 2.连数据库
			con = JDBCUtil.getConn();
			// 3.获取数据库操作对象(SQL)
			stmt = con.createStatement();
			// 4.操作数据库(查询)
			String sql = "insert into user() values(" + id + ",'" + u[1]
					+ "','" + u[2] + "','" + u[3] + "','" + pk + "')";
			stmt.execute(sql);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("操作失败");
			return "fail";
		} finally {
			// 6.关闭JDBC对象
			JDBCUtil.close(rs, stmt, con);
		}
		return id + "," + pk;
	}

	/**
	 * 用户登录
	 * 
	 * @param name
	 * @param password
	 * @return 0:success;<br>
	 *         1:failed, Because IOException;<br>
	 *         2:failed, this name is not exist;<br>
	 *         3:failed, this password is wrong;<br>
	 */
	public boolean login(String user) {
		String[] u = user.split(",");
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			// 2.连数据库
			con = JDBCUtil.getConn();
			// 3.获取数据库操作对象(SQL)
			stmt = con.createStatement();
			// 4.操作数据库(查询)
			String sql = "select * from user where name = '" + u[1]
					+ "' and password = '" + u[2] + "'";
			rs = stmt.executeQuery(sql);
			if (rs.next()) {
				return true;
			}
		} catch (Exception e) {
			// e.printStackTrace();
			System.out.println("操作失败");
		} finally {
			// 6.关闭JDBC对象
			JDBCUtil.close(rs, stmt, con);
		}
		return false;
	}

	/**
	 * 检查用户是否存在
	 * 
	 * @param userName
	 * @param password
	 * @return
	 */
	public boolean checkUserExist(String name) {
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			// 2.连数据库
			con = JDBCUtil.getConn();
			// 3.获取数据库操作对象(SQL)
			stmt = con.createStatement();
			// 4.操作数据库(查询)
			String sql = "select * from user where name = '" + name + "'";
			rs = stmt.executeQuery(sql);
			if (rs.next()) {
				return true;
			}
		} catch (Exception e) {
			// e.printStackTrace();
			System.out.println("操作失败");
		} finally {
			// 6.关闭JDBC对象
			JDBCUtil.close(rs, stmt, con);
		}
		return false;
	}
}
